package com.gjy.stream;

import com.gjy.util.StreamUtil;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2022-12-07 13:42:08
 */
public class T1Test {

    @Test
    public void test3() {
        List<Integer> numbers = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
        List<Integer> page = StreamUtil.page(numbers, 3, 5);
        System.out.println("page = " + page);
    }

    @Test
    public void test2() {
        int num = 2;
        int size = 10;
        List<Integer> numbers = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
        List<Integer> collect = numbers.stream()
                .skip((num - 1) * size)
                .limit(size)
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);
    }

    @Test
    public void test1() {
        List<Integer> numbers = asList(1, 2, 3, 4);
        String collect = numbers.stream()
                .map(String::valueOf)
                .collect(Collectors.joining("-", "[", "]"));
        System.out.println("collect = " + collect);

        StringBuilder reduce = numbers.stream()
                .map(String::valueOf)
                .reduce(new StringBuilder(), (builder, value) -> {
                    if (builder.length() > 0) {
                        builder.append(",");
                    }
                    builder.append(value);
                    return builder;
                }, StringBuilder::append);

        StringJoiner reduce1 = numbers.stream()
                .map(String::valueOf)
                .reduce(new StringJoiner(",", "", ""),
                        StringJoiner::add,
                        StringJoiner::merge);
        assertEquals(reduce.toString(), reduce1.toString());
    }
}
